package libra.controller;

import java.util.Map;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;

/**
 * Sentinel整合Gateway - Flow流控.请求属性测试Controller
 */
@RestController
@RequestMapping("reqAttr")
public class RequestAttrController {
	String from = getClass().getSimpleName();
	
	
	/************************** 流控.针对请求属性测试 ******************************/
	/**
	 * 测试'针对请求属性.ClientIP'，不配置'属性值配置'
	 * !!!：所有ip来源的请求均会执行限流规则 
	 */
	@RequestMapping("testClientIpWithoutMatchAttr")
	public Object testClientIpWithoutMatchAttr() {
		return Map.of("from", from, "method", "testClientIpWithoutMatchAttr");
	}
	/**
	 * 测试'针对请求属性.ClientIP'，配置'属性值配置'
	 * !!!：只有与'属性值匹配'规则匹配的ip来源的请求才会执行限流规则
	 */
	@RequestMapping("testClientIpWithMatchAttr")
	public Object testClientIpWithMatchAttr() {
		return Map.of("from", from, "method", "testClientIpWithMatchAttr");
	}
	
	
	/**
	 * 测试'针对请求属性.Header'，不配置'属性值配置'
	 * !!!：所有携带有指定请求头key的请求，均会执行限流规则
	 */
	@RequestMapping("testHeaderWithoutMatchAttr")
	public Object testHeaderWithoutMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testHeaderWithoutMatchAttr", "headers", req.getHeaders());
	}
	/**
	 * 测试'针对请求属性.Header'，配置'属性值配置'
	 * !!!：携带有指定请求头key，且请求头的val值与'属性值匹配'规则匹配的请求才会执行限流规则
	 */
	@RequestMapping("testHeaderWithMatchAttr")
	public Object testHeaderWithMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testHeaderWithMatchAttr", "headers", req.getHeaders());
	}
	
	
	/**
	 * 测试'针对请求属性.URL参数'，不配置'属性值配置'
	 * !!!：所有携带有指定请求参数key的请求，均会执行限流规则
	 */
	@RequestMapping("testParamWithoutMatchAttr")
	public Object testParamWithoutMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testParamWithoutMatchAttr", "params", req.getQueryParams() );
	}
	/**
	 * 测试'针对请求属性.URL参数'，配置'属性值配置'
	 * !!!：携带有指定指定请求参数key，且请求参数的val值与'属性值匹配'规则匹配的请求才会执行限流规则
	 */
	@RequestMapping("testParamWithMatchAttr")
	public Object testParamWithMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testParamWithMatchAttr", "params", req.getQueryParams());
	}
	
	
	/**
	 * 测试'针对请求属性.URL参数'，不配置'属性值配置'
	 * !!!：所有携带有指定请求Cookie_key的请求，均会执行限流规则
	 */
	@RequestMapping("testCookieWithoutMatchAttr")
	public Object testCookieWithoutMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testCookieWithoutMatchAttr", "cookis", req.getCookies() );
	}
	/**
	 * 测试'针对请求属性.URL参数'，配置'属性值配置'
	 * !!!：携带有指定指定请求Cookie_key，且请求Cookie_val值与'属性值匹配'规则匹配的请求才会执行限流规则
	 */
	@RequestMapping("testCookieWithMatchAttr")
	public Object testCookieWithMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testCookieWithMatchAttr", "cookis", req.getCookies());
	}
	
	
	// ???
	@RequestMapping("testRemoteHostWithoutMatchAttr")
	public Object testRemoteHostWithoutMatchAttr( ServerHttpRequest req, ServerHttpResponse resp, ServerWebExchange serverWebExchange ) {
		return Map.of("from", from, "method", "testRemoteHostWithoutMatchAttr", "cookis", req.getRemoteAddress() );
	}
}
